package com.qike.duola.controller;

import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.IndexService;
import com.qike.duola.service.TrainService;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "培训控制器")
@CrossOrigin
public class TrainController {

    private static Logger logger = LoggerFactory.getLogger(TrainController.class);

    @Autowired
    private TrainService trainService;


    /**
     * 培训列表
     *
     * @return
     */
    @ApiOperation("培训列表")
    @GetMapping("/trainList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TrainListVo.class)
    })
    public BaseResult trainList(TrainListDto trainListDto) {
        try {
            TrainListVo trainList = trainService.trainList(trainListDto);
            return RetResponse.makeOKRsp(trainList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 发布培训
     *
     * @return
     */
    @ApiOperation("发布培训")
    @PostMapping("/addTrain")
    @ResponseBody

    public BaseResult addTrain(@RequestBody AddTrainDto addTrainDto) {
        try {
            trainService.addTrain(addTrainDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 培训详情
     *
     * @return
     */
    @ApiOperation("培训详情")
    @GetMapping("/trainInfo")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TrainInfoVo.class)
    })
    public BaseResult trainInfo(Integer id) {
        try {
            TrainInfoVo vo = trainService.trainInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 培训编辑
     *
     * @return
     */
    @ApiOperation("培训编辑")
    @PostMapping("/trainEdit")
    @ResponseBody
    public BaseResult trainEdit(@RequestBody TrainEditDto trainEditDto) {
        try {
            trainService.trainEdit(trainEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 发布/取消发布
     *
     * @return
     */
    @ApiOperation("发布/取消发布")
    @PostMapping("/trainOpenOrClose")
    @ResponseBody
    public BaseResult trainOpenOrClose(@RequestBody TrainOpenOrCloseDto trainOpenOrCloseDto) {
        try {
            trainService.trainOpenOrClose(trainOpenOrCloseDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除培训
     *
     * @return
     */
    @ApiOperation("删除培训")
    @PostMapping("/trainDel")
    @ResponseBody
    public BaseResult trainDel(@RequestBody TrainDelDto trainDelDto) {
        try {
            trainService.trainDel(trainDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 参加培训
     *
     * @return
     */
    @ApiOperation("参加培训")
    @PostMapping("/trainJoin")
    @ResponseBody
    public BaseResult trainJoin(@RequestBody TrainJoinDto trainJoinDto) {
        try {
            trainService.trainJoin(trainJoinDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 取消培训
     *
     * @return
     */
    @ApiOperation("取消培训")
    @PostMapping("/trainCancel")
    @ResponseBody
    public BaseResult trainCancel(@RequestBody TrainCancelDto trainCancelDto) {
        try {
            trainService.trainCancel(trainCancelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 参与名单列表
     *
     * @return
     */
    @ApiOperation("参与名单列表")
    @GetMapping("/trainUserList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TrainUserListVo.class)
    })
    public BaseResult trainUserList(Integer id) {
        try {
            List<TrainUserListVo> voList = trainService.trainUserList(id);
            return RetResponse.makeOKRsp(voList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 参与名单编辑得分
     *
     * @return
     */
    @ApiOperation("参与名单编辑得分")
    @PostMapping("/trainUserEdit")
    @ResponseBody
    public BaseResult trainUserEdit(@RequestBody TrainUserEditDto trainUserEditDto) {
        try {
            trainService.trainUserEdit(trainUserEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
